﻿
/*======================================================================================================\
|                                          空调配送增加、修改页面                                                 |
|-------------------------------------------------------------------------------------------------------|
|作者：郭建文                                                                                           |
|时间：2010-09-09 创建并注释                                                                            |
|                                                                                                       |                                  |
|                                                                                                       |
|-------------------------------------------------------------------------------------------------------|
|                                                                                                       |
|                 必须严格执行代码层次缩进规范！4空格缩进！严禁tab键缩进！                              |
|                                                                                                       |
|                                       Copyright (c) 2009 YuJunJie                                     |
\======================================================================================================*/
//=======================================================================================
//定义一个以简单模式布局的页面，简单模式的page对象，包含一个frame页面结构对象
//frame页面结构对象包含其子元素 ：查询按钮、表单、表格
//=======================================================================================
var page1 = new Sail.page({
    
	name     : "pageDetail",                      //定义页面对象的唯一标识
    haveNavi : false,                           //未包含导航条

    frames   : {
	
        name     : "editFrame",                     //该frame对象的唯一标识
        title  : "空调配送单数据维护",
        getURL : "../../distribution.do?method=edit",
        getUrlParameter : ["id"],
        postURL : "../../distribution.do?method=save",
        bindDataFirst : false,
        
      //-------------------------------------------------------------------------------
        //以JSON形式描述 frame 的构成(包含哪些 item)
        //-------------------------------------------------------------------------------
        items  : [
            {
                type  : "form", 
                name  : "formDetail",
                title : "空调配送单据",
                
                buttons : ["保存","关闭"],
            
                items : [
                    {
				        type:"input",
				        title:"单据编号",
				        allowBlank : false,
				        readOnly:true,
				        dataIndex:"dataset.billVo.billCode"
				    },
				    {
				        type:"calendar",
				        title:"单据日期",
				        allowBlank : false,   
				        dataIndex:"dataset.billVo.billDate"
				    },
				    {
		                type:"combox", 
		                title:"门店",
		                dataIndex : "dataset.billVo.otherDic2Id",
		                nameIndex : "dataset.billVo.otherDic2Name",    
		                allowBlank:true,
		                root:"dataset.item",
		                fieldText:"name",
		                fieldValue:"id",
		                url:"../../../DataDict?type=repairsSupplier"
		            },{
						type:"combox", 
						title:"资金形式",
						dataIndex : "dataset.billVo.otherDic1Id",
						nameIndex : "dataset.billVo.otherDic1Name",    
						allowBlank:false,
						root:"dataset.dictionary",
						fieldText:"name",
						fieldValue:"id",
						url:"../../dictionary.do?method=findDictionaryByTypeId&typeId="+COMBOX_PRICE_TYPE
	                },
//	                ,{
//				        type:"input",
//				        title:"录入人",
//				        readOnly:true,
//				        dataIndex:"dataset.outwarehouseinfovo.baseInfo.creatorName"
//				    },
//				    {
//				        type:"input",
//				        title:"审批人",
//				        readOnly:true,
//				        dataIndex:"dataset.outwarehouseinfovo.baseInfo.auditUserName"
//				    },{
//				        type:"input",
//				        title:"确认人",
//				        readOnly:true,
//				        dataIndex:"dataset.outwarehouseinfovo.baseInfo.affirmUserName"
//				    },
				    {
				        type:"textArea",
				        title:"备注",
				        dataIndex:"dataset.billVo.remark",
				        height:50	
				            
				    }
                ],
            
                layoutConfig : {
                    columns : 2
                },
                layout : [
	                    {
				            items:[
				                {'name':'单据编号'},
				                {'name':'单据日期'}
				            ]
				        },{
				            items:[
				                {'name':'门店'},
				                {'name':'资金形式'}
				            ]
				        },
//				        {
//				            items:[
//					             {'name':'审批人'},
//					             {'name':'确认人'}
//					        ]
//					    },
					    {
				            items:[
				                {name:'备注','colspan':2}
				            ]
				        }
                ]
            },
            {
                name      : "gridSub",
                type      : "editGrid",
                title     : "空调配送单明细",
              
                tbar      : ['新增','-','删除','-'],
                bbar      : false,
                allowBlank  : false,
                useDefautlFuncForadd : false,
                bindRelation   : {
                    root  :"dataset.billVo.billDetailVo",
                    id    :"id",
                    field : [
                                
                    ],
                    col   :[
            				{"header":"空调Id",   "width":200,   align:"left", "dataIndex":"materielId",hidden:true},
						    {"header":"空调名称",   "width":200,   align:"left", "dataIndex":"materielName"},
						    {"header":"供给形式",   "width":70, align:"left", "dataIndex":"otherDic1Id","nameIndex":"otherDic1Name"},
						    {"header":"数量",   "width":80,   align:"left","dataIndex":"amount"},
						    {"header":"单价",   "width":80,   align:"left","dataIndex":"price"},
						    {"header":"金额",   "width":80,   align:"left","dataIndex":"money"},
						    {"header":"单位Id",   "width":200,   align:"left", "dataIndex":"unitId",hidden:true},
						    {"header":"单位",   "width":60,   align:"left","dataIndex":"unitName"} 
						   // {"header":"现存量",   "width":60,   align:"left","dataIndex":"stock"}
						    /*,
						    {"header":"金额",   "width":80,   align:"left","dataIndex":"showMoney"},
						    {"header":"单价",   "width":80,   align:"left","dataIndex":"price"}*/
                    ],
                    "editor":[
                        {},
                        {},
                        {type:"comboBox",config:{url:"../../dictionary.do?method=findDictionaryByTypeId&typeId="+COMBOX_SUPPLY_TYPE,fieldText:"name",fieldValue:"id",root:"dataset.dictionary","allowBlank":false}},
                        {type:"numberField", config:{allowBlank:false, maxLength:MAXLENGTH,decimalPrecision:DECIMALPRECISION}},
                        {type:"numberField", config:{allowBlank:false, maxLength:MAXLENGTH,decimalPrecision:DECIMALPRECISION}},
                        {},
                        {},
                        {type:"comboBox",config:{url:"../../dictionary.do?method=findDictionaryByTypeId&typeId="+DICTIONARY_TYPE_MATERIAL_UNIT,fieldText:"name",fieldValue:"id",root:"dataset.dictionary","allowBlank":false}}/*,
                        {},
                        {type:"numberField", config:{allowBlank:false, maxLength:MAXLENGTH,decimalPrecision:DECIMALPRECISION}},
                        {}*/
                    ]
                }
            }
        ],
        
        //-------------------------------------------------------------------------------
        //【注】 添加 businessData 空结构
        //-------------------------------------------------------------------------------
        businessData : {
    "dataset":{
    "billVo":{
      "id":"",    
      "billDate":"",
      "operator":"",
      "transactionId":"",
      "transactionTypeId":TRANSACTION_TYPE_ADD,
      "billTypeId":BILL_TYPE_DISTRIBUTION,
      "billCode":"",
      "fillTime":"",
      "billTypeName":"",
      "originEntityName":"",
      "targetEntityName":"",
      "originEntityId":"",
      "targetEntityId":"",        
      "otherDic1Id":"",
      "otherDic2Id":"",
      "otherDic3Id":"",
      "otherDic4Id":"",
      "otherDic5Id":"",
      "otherDic6Id":"",
      "otherDic7Id":"",
      "otherDic8Id":"",
      "otherDic9Id":"",
      "otherDic10Id":"",   
      "otherDic1Name":"",
      "otherDic2Name":"",
      "otherDic3Name":"",
      "otherDic4Name":"",
      "otherDic5Name":"",
      "otherDic6Name":"",
      "otherDic7Name":"",
      "otherDic8Name":"",
      "otherDic9Name":"",
      "otherDic10Name":"",
      "remark":"",
      "sysFlag":SYS_FLAG_USEING,
      "creator":"",
      "createTime":"",
      "lastModifier":"",
      "lastModifiedTime":"",
      "billDetailVo":
        {
          "id":"",
          "transactionDetailTypeId":TRANSACTION_DETAIL_TYPE_OUT,
          "unitId":COMBOX_UNIT_VAULE,
          "unitName":COMBOX_UNIT_NAME,
          "materielId":"",
          "materielName":"",
          "price":"",
          "money":"",
          "amount":"",    
          "stateId":"",
          "stateName":"",
          "weight":"",
          "originEntityId":"",
          "targetEntityId":"",
          "originEntityName":"",
          "targetEntityName":"",
          "otherDic1Id":"",
          "otherDic2Id":"",
          "otherDic3Id":"",
          "otherDic4Id":"",
          "otherDic5Id":"",
          "otherDic6Id":"",
          "otherDic7Id":"",
          "otherDic8Id":"",
          "otherDic9Id":"",
          "otherDic10Id":"",
          "otherDic1Name":"",
          "otherDic2Name":"",
          "otherDic3Name":"",
          "otherDic4Name":"",
          "otherDic5Name":"",
          "otherDic6Name":"",
          "otherDic7Name":"",
          "otherDic8Name":"",
          "otherDic9Name":"",
          "otherDic10Name":"",
          "remark":"",
          "sysFlag":SYS_FLAG_USEING,
          "creator":"",
          "createTime":"",
          "lastModifier":"",
          "lastModifiedTime":""
        }
      
    }
  }
        },

        //-------------------------------------------------------------------------------
        //【注】为当前 frame 的items 绑定事件
        //-------------------------------------------------------------------------------
        bindEvent2Items : function(){    
        	var obj_grid = $('gridSub'); 
            var obj_form  = $('formDetail');

            $('formDetail').getButton('保存').on('click',function(){
            	var gridData = obj_grid.el.getStore().data.items;
                this.saveBusiness("close");
            },this);
            
            obj_grid.addListener2Editor("数量",'afteredit',function(column,editInfo){
            	computeRowMoney(editInfo);
            });
            
            obj_grid.addListener2Editor("单价",'afteredit',function(column,editInfo){
            	computeRowMoney(editInfo);
            });
            
            $('formDetail').getButton('关闭').on('click',function(){
                this.close();
            },$('formDetail').ownerFrame);
            
            
            obj_grid.addButtonHandler('新增',function(){
                var items = Sail.open("materielSelector.html","list",{},600,520);
                if(!items || items.length===0) return;
                var obj_grid = this;
                var ids = [];
                for(var i=0;i<items.length;i++)
                {
                	//记录过滤
                	var alreadyExist = false;
        			var exist = this.el.getStore().data.items;
        			for (var j = 0, count = exist.length; j < count; j++){
        				if (exist[j].get("materielId") == items[i].data.pid)
        					 alreadyExist = true;
        			}
        			if (alreadyExist) continue;
        					
                	var record = obj_grid.createRecord.call(obj_grid);
		            record.editing = true;
		            //record.set('repairsTypeName', items[i].data.typeName || '');
		            record.set("transactionDetailTypeId",TRANSACTION_DETAIL_TYPE_OUT);
	                  record.set("sysFlag",SYS_FLAG_USEING);
			              record.set('materielName', items[i].data.name || '');
			              record.set('materielId', items[i].data.id);
			              record.set('unitName', items[i].data.other2Name || '');
			              record.set('unitId', items[i].data.other2Id);
			              record.editing = false;
		            obj_grid.addRecord.call(obj_grid,record);
                  //  ids.push(items[i].data.pid || '');
                }
            },obj_grid);
        },
        
        beforeBindData:function()
        {
        	if(this.getRequest("isCopyAdd") == 'true'){
        		var bill = this.businessData.dataset.billVo;
        		bill.id = "";
        		bill.transactionId = "";
        		bill.billDate = "";
        		bill.billCode = "";
        		var detailList = this.businessData.dataset.billVo.billDetailVo;
          	for (var i = 0; i < detailList.length; i++){
          		var detail = detailList[i];
          		detail.id = "";
          		detail.transactionId = "";
          	}
          }
        	return true;
        }
    },

	afterRun : function(){
    	$('单据编号').disable();
    	//$('录入人').disable();
    	//$('审批人').disable();
    	//$('确认人').disable();
    	
    	//增加时执行
    	if(page1.isAdd() || page1.getRequest().data.isCopyAdd=='true') {
    		 setBillCode();
	    }
	}
  });

page1.run();

function computeRowMoney(gridRow)
{
	var amount = gridRow.record.get("amount");
    var price = gridRow.record.get("price");
    var money = 0;
    if (amount != 0)
    {
    	money = Math.round(100 * price * amount) / 100; //保留2位小数
    }
    
    $('gridSub').setFieldValueByDataIndex("money", gridRow.row, money);
}
/**
 * 设置单据编码
 * @return
 */
function setBillCode(currDate){
	var url = "../../distribution.do?method=randomBillCode&billType="+BILL_TYPE_DISTRIBUTION;
	Sail.postData(url,{},function(flag,getData){
		$('单据编号').setValue(getData.dataset.billCode);
	});
}